import chart from '@/utils/chart.js'
import { getDateList } from '@/utils/timeUtil'
import { isNo, parseJson } from '@/utils/tool'
import { COFFEE_TYPE } from '@/consts/codeMaps'
const { clientWidth:w } = document.body

function transProd (k) {
  if (k === 0) {
    return '销售总量'
  } else {
    return COFFEE_TYPE[k]
  }
}

export const chartSales = (chartData, time_start, time_end, selCoffee, selectList,echarts) => {

  let xData = []
  let total = 0

  let series = []
  let selectCoffee = []
  if (selCoffee.indexOf(0) >= 0) {
    for (let coffee of selectList) {
      if (coffee.value !== 0) {
        selectCoffee.push(coffee.value)
      }
    }
  } else {
    selectCoffee = selCoffee
  }

  function seriesPush(prod) {
    let o = {
      data: [],
      ...chart.BAR_CONFIG,
      stack: '总量',
      name: transProd(prod.formula.toString()),
      barWidth: w/1920 * 30,
      itemStyle:{
        barBorderRadius:[w/1920 * 30, w/1920 * 30, 0, 0],  
      },
      color: new echarts.graphic.LinearGradient(  //渐变色
        0, 0, 0, 1,
        [
            {offset: 0, color: 'rgba(113,197,241,0.8)'},
            {offset: 0.5, color: 'rgba(92,154,191,0.8)'},
            {offset: 1, color: 'rgba(61,92,120,0.8)'}
        ]
      ),
    }
    o.data.push([
      transProd(prod.formula),
      prod['pCount']
    ])
    xData.push(transProd(prod.formula))
    
    series.push(o)
  }

  let max = 0
  if (chartData.length > 0) {
    for (let i = 0; i < chartData.length; i++) {
      let item = chartData[i]

      if (!isNo(item.data)) {
        const data = parseJson(item.data)
        total = data.total
        if (selCoffee.indexOf(0) >= 0) {
          let sum = 0
          xData = []
          for (let prod of data.products) {
            seriesPush(prod)
            sum += prod['pCount']
          }
          max = sum > max ? sum : max
        } else {
          let sum = 0
          xData = []
          for (let prod of data.products) {
            if (selCoffee.indexOf(prod.formula.toString()) >= 0) {
              seriesPush(prod)
              sum += prod['pCount']
            }
          }
          max = sum > max ? sum : max
        }
      }

      series.sort((a, b) => {
        return a.data[0][1] - b.data[0][1]
      })

    }
  } else {
    for (let f of xData) {
      for (let k = 0; k < series.length; k++) {
        const seriesItem = series[k]
        seriesItem.data.push([f, 0])
      }
    }
  }

  let tooltipConfig = null
  tooltipConfig = chart.TOOLTIP
  
  let option = {
    color: ['#71c5f1'],
    grid: chart.DETAIL_GRID,
    // legend: chart.DETAIL_LEGEND,
    tooltip: {
      ...tooltipConfig,
      axisPointer: {
        type: ''
      },
      formatter: (params, ticket) => {
        let s = ''
        s = s + params[0].axisValue + `<br/>`
        const aParams = params.reverse()
        for (let item of aParams) {
          const saleCount = item.value[1]
          if (saleCount > 0) {
            s = s + `<span style="color: rgba(113,197,241)">
                  <div class="tooltip-xxl">${'销售统计'}: ${saleCount} ${'杯'}</div></span>`
          }
        }
        return s
      }
    },
    grid: {
      top:'50',
      left: '3%',
      right: '2%',
      bottom: '4%',
      containLabel: true
    },
    dataZoom: [
      {
        type: 'inside',
        throttle: 100
      }
    ], 
    xAxis: {
      splitLine: {
        show: false
      },
      data: xData,
      type: 'category',
      ...chart.BAR_XAXIS_STYLE
    },
    yAxis: {
      name: '杯       ',
      type: 'value',
      minInterval: 1,
      ...chart.AXIS_STYLE,
      // max: function (value) {
      //   return max + 20
      // }
    },
    series
  }
  console.log('chartSales:',series)
  return [option, total]
}